package com.yuaer.demo.springsparkjobhandlerdemo.executor.processor;

import com.yuaer.demo.springsparkjobhandlerdemo.executor.GlobalContext;
import com.yuaer.demo.springsparkjobhandlerdemo.executor.NodeContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import static org.apache.spark.sql.functions.col;

public class CycleNodeProcessor implements NodeProcessor{

    @Override
    public Dataset<Row> process(GlobalContext context, NodeContext nodeContext) {

        Map<String, Object> params = nodeContext.getParams();
        LocalDate startDate = (LocalDate) params.get("startDate");
        LocalDate endDate = (LocalDate) params.get("endDate");

        SparkSession sparkSession = context.getSpark();
        Dataset<Row> batchResult = nodeContext.getInputs().values().stream()
                .reduce((d1, d2) -> d1.union(d2))
                .orElse(sparkSession.emptyDataFrame());
        // TODO: 2021/3/30 这里可能需要将LocalDate 转为字符串或java.sql.Date类型
        // 参考 java.sql.Date.valueOf(localDate)
        return  batchResult.filter(col("date").between(startDate, endDate));
    }

}
